package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.SparePartDao;
import com.sugon.dao.SparePartRequDetailDao;
import com.sugon.entity.SparePartRequDetailEntity;
import com.sugon.modules.sparepart.model.vo.SparePartRequDetailVO;
import com.sugon.service.SparePartRequDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 备件需求计划明细Service实现类
 *
 * @author sugon
 * @date 2019-08-02 14:31:17
 */
@Service("sparePartRequDetailService")
public class SparePartRequDetailServiceImpl implements SparePartRequDetailService {

    @Autowired
    private SparePartRequDetailDao sparePartRequDetailDao;
    @Autowired
    private SparePartDao sparePartDao;

    @Override
    public List<SparePartRequDetailVO> listByPlanId(String planId) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("planId", planId);
        return queryList(param);
    }

    @Override
    public SparePartRequDetailVO queryObject(String id) {
        return sparePartRequDetailDao.queryById(id);
    }

    /**
     * 校验指定需求计划中是否存在特定物料编码
     *
     * @param needsPlanCode 需求计划编码
     * @param materialCode  物料编码
     * @return
     */
    @Override
    public boolean isExists(String needsPlanCode, String materialCode) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("planCode", needsPlanCode);
        param.put("materialCode", materialCode);
        return queryTotal(param) > 0;
    }

    /**
     * @param map
     * @return
     */
    @Override
    public Double countTotalMoney(Map<String, Object> map) {
        return sparePartRequDetailDao.countTotalMoney(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<SparePartRequDetailVO> queryList(Map<String, Object> map) {
        return sparePartRequDetailDao.listAll(map);
    }

    /**
     * @param sparePartNeedsPlanCode 备件需求计划编码
     * @return
     */
    @Override
    public List<SparePartRequDetailVO> queryByPlanCode(String sparePartNeedsPlanCode) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("planCode", sparePartNeedsPlanCode);
        return queryList(param);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return sparePartRequDetailDao.queryTotal(map);
    }

    /**
     * @param sparePartRequDetail 实体
     * @return
     */
    @Override
    public int save(SparePartRequDetailEntity sparePartRequDetail) {
        sparePartRequDetail.setId(RandomUtil.randomString(32));
        sparePartRequDetail.setGmtCreate(DateUtil.date());
        sparePartRequDetail.setGmtModified(sparePartRequDetail.getGmtCreate());
        if (sparePartRequDetail.getInboundNum() == null) {
            sparePartRequDetail.setInboundNum(0);
        }
        if (sparePartRequDetail.getTransitNum() == null) {
            sparePartRequDetail.setTransitNum(0);
        }
        if (sparePartRequDetail.getIsCancelTransit() == null) {
            sparePartRequDetail.setIsCancelTransit(0);
        }
        return sparePartRequDetailDao.save(sparePartRequDetail);
    }

    /**
     * @param sparePartRequDetail 实体
     * @return
     */
    @Override
    public int update(SparePartRequDetailEntity sparePartRequDetail) {
        sparePartRequDetail.setGmtModified(DateUtil.date());
        return sparePartRequDetailDao.update(sparePartRequDetail);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return sparePartRequDetailDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return sparePartRequDetailDao.deleteBatch(ids);
    }

    /**
     * @param sparePartNeedsPlanCode 备件需求计划编码
     */
    @Override
    public void updateNeedsItemTransitNumByNeedsId(String sparePartNeedsPlanCode) {
        List<SparePartRequDetailEntity> needsItemList = sparePartRequDetailDao.listByPlanCode(sparePartNeedsPlanCode);
        if (CollectionUtil.isNotEmpty(needsItemList)) {
            for (SparePartRequDetailEntity needsItem : needsItemList) {
                needsItem.setTransitNum(needsItem.getDemandNum());
                if (update(needsItem) > 0) {
                    // 实时更新备件在途数
                    sparePartDao.realtimeUpdateTransitNum(needsItem.getMaterialCode());
                }
            }
        }
    }
}
